<template>
    <div class="app-container">
        <div style="text-align: center;margin-bottom: 30px">
            每日任务更新
        </div>
        <el-form :model="dailyTaskSForm" ref="dailyTaskSForm"  :rules="dailyTaskSFormRules" label-width="100px">
            <el-row :gutter="10">
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="任务名称" prop="taskName">
                        <el-input type="text" v-model="dailyTaskSForm.taskName" placeholder="请输入任务名称"
                                  clearable></el-input>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="所属主体" prop="enterpriseId" >
                        <el-select v-model="dailyTaskSForm.enterpriseId" placeholder="请选择主体" style="width: 100%" @change="getInUserList">
                            <el-option v-for="(item,index) in enterpriseDominantList" :key="index" :value="item.ucId"
                                       :label="item.name"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
              <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                <el-form-item label="所属区域" prop="regionId">
                  <el-select v-model="dailyTaskSForm.regionId" placeholder="请选择所属区域" style="width: 100%">
                    <el-option v-for="(item,index) in regionList" :key="index" :value="item.ucId" :label="item.name"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
                <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                    <el-form-item label="执行人员" prop="userId">
                        <el-select v-model="dailyTaskSForm.userId" placeholder="请选择执行人员" style="width: 100%">
                            <el-option v-for="(item,index) in userList" :key="index" :value="item.ucId"
                                       :label="item.disPlayName"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
              <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl=12>
                <el-form-item label="任务开始时间" prop="startTime">
                  <el-time-picker style="width: 100%"
                                  v-model="dailyTaskSForm.startTime"
                                  :picker-options="{
                                       selectableRange: '00:00:00 - 23:59:59'
                                      }"
                  >
                  </el-time-picker>
                </el-form-item>

              </el-col>
              <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl=12>
                <el-form-item label="任务结束时间" prop="endTime">
                  <el-time-picker style="width: 100%"
                                  v-model="dailyTaskSForm.endTime"
                                  :picker-options="{
                                       selectableRange: '00:00:00 - 23:59:59',
                                       minTime:dailyTaskSForm.startTime
                                }"
                  >
                  </el-time-picker>
                </el-form-item>
              </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                    <el-form-item label="任务内容" prop="content">
                        <el-input  type="textarea" :rows="2" v-model="dailyTaskSForm.content" placeholder="请输入任务内容" clearable></el-input>
                    </el-form-item>
                </el-col>

            </el-row>
        </el-form>
        <div slot="footer" style="text-align: center" class="dialog-footer">
            <el-button @click="cancelUserTask">取 消</el-button>
            <el-button :loading="addLoading"  type="primary" @click="updateUserTask">修 改</el-button>
        </div>
    </div>
</template>

<script>
import {getInDailyTasksVo,addInDailyTasks,updateInDailyTasks} from "@/api/dailyTasks";
import {getInEnterpriseList,getInUserList} from "@/api/enterprise";
import {getInEnterpriseDominantList} from "@/api/enterpriseDominant";
import {getInRegionList} from "@/api/region";


export default {
    name: "UserTaskEdit",
    data() {
        return {
            dailyTaskSForm: {
                status:0,
            },
            addLoading: false,
            enterpriseDominantList:[],
            regionList:[],
            userList:{},
            dailyTaskSFormRules:{
                name: [{required: true, message: '任务名称不能为空', trigger: 'blur'}],
                content: [{required: true, message: '任务内容不能为空', trigger: 'blur'}],
                enterpriseId :[{required: true, message: '所属企业不能为空', trigger: 'blur'}],
                userId :[{required: true, message: '执行人员不能为空', trigger: 'blur'}],
            },
        }
    },
    computed: {
    },
    created() {
        // this.getInUserList()
        this.getInEnterpriseDominantList()
        this.getInDailyTasksVo(this.$route.params.ucId)
        this.getInRegionList()
    },
    methods: {
        getInDailyTasksVo(val) {
            getInDailyTasksVo({ucId: val}).then(res => {
                this.dailyTaskSForm = res.data
                this.getInUserList(this.dailyTaskSForm.enterpriseId)
            }).catch(res => {

            })
        },
        cancelUserTask() {
          window.history.back()
        },
        createUserTask() {
            this.addLoading = true
            this.$refs['dailyTaskSForm'].validate(valid => {
                if (valid) {
                    addInDailyTasks(this.dailyTaskSForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加每日成功"
                        })
                        this.cancelUserTask()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updateUserTask() {
            this.addLoading = true
            this.$refs['dailyTaskSForm'].validate(valid => {
                if (valid) {
                    updateInDailyTasks(this.dailyTaskSForm).then(res => {
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新每日任务成功"
                        })
                        this.cancelUserTask()
                    }).catch(res => {
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                } else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        getInEnterpriseDominantList(){
            getInEnterpriseDominantList({deleted:false}).then(res=>{
                this.enterpriseDominantList = res.data
            }).catch(error=>{

            })
        },
        getInUserList(val) {
            getInUserList({flagId:val,deleted: false}).then(res => {
                this.userList = res.data
            }).catch(error => {

            })
        },
      getInRegionList(val){
        getInRegionList({}).then(res=>{
          this.regionList = res.data
        }).catch(error=>{
          console.log(error)
        })
      },


    }

}
</script>
